//
//  UIStrokeView.h
//  StrokeDemo
//
//  Created by wankang on 2/13/15.
//  Copyright (c) 2015 wankang. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol PlaySoundEndNotify <NSObject>
-(void)onPlayEnd;
@end

@protocol PlayChnSoundDelegate <NSObject>
-(void)playSound:(NSString *)sChn forStroke:(BOOL)fStroke withNotify:(id<PlaySoundEndNotify>)notify;
@end

@protocol StrokeDataDelegate <NSObject>
-(NSArray *)getStrokeForChn:(NSString *)sChn width:(NSInteger)maxWidth height:(NSInteger)maxHeight;
@optional
-(NSArray *)getGuideForChn:(NSString *)sChn withContourIndex:(NSInteger)index width:(NSInteger)maxWidth height:(NSInteger)maxHeight;
@end

typedef enum tagStrokeBack {
    BACK_NONE,
    BACK_GRID_KOU,
    BACK_GRID_TIAN,
    BACK_GRID_MI,
    BACK_GRID_JING,
}StrokeBack;

/** UIView class for chn-stroke showing/filling.
 *
 * color property in type of NSInteger:
 * 0xff000000 for alpha;
 * 0x000000ff for red;
 * 0x0000ff00 for green;
 * 0x00ff0000 for blue;
 */
@interface UIStrokeView : UIView {
    NSString    *mCode;         // chn code
    NSArray     *mRecord;       // stroke record array
    int         mLeftMargin;    // left-margin in this UIView
    int         mTopMargin;     // top-margin in this UIView
    NSThread    *mFillThread;   // thread to fill stroke
    UIImage     *mShowImage;    // image for filling thread
    NSArray     *mStartPoint;   // start point list for digit index
    BOOL        mStopThread;    // flag for stopping thread quickly
    BOOL        mThreadFinish;  // thread procedure is finished
}

/** to draw stroke contour or not
 * default: YES
 */
@property (nonatomic) BOOL  drawContour;
/** to fill stroke contour or not
 * default: NO
 */
@property (nonatomic) BOOL  fillContour;
/** to draw middle line or not
 * default: NO
 */
@property (nonatomic) BOOL  drawMidline;
/** to draw digit index or not
 * default: YES
 */
@property (nonatomic) BOOL  drawIndex;
/** to play stroke sound or not
 * default: YES
 */
@property (nonatomic) BOOL  playStrokeWave;
/** to play Chinese sound or not
 * default: YES
 */
@property (nonatomic) BOOL  playChnWave;

/** maximum width, default 267
 */
@property (nonatomic) NSInteger maxWidth;
/** maximum height, default 267
 */
@property (nonatomic) NSInteger maxHeight;
/** background color
 */
@property (nonatomic) NSInteger clrBack;
/** color for lines in background
 */
@property (nonatomic) NSInteger clrLine;
/** color for BuShou(radical part)
 */
@property (nonatomic) NSInteger clrBuShou;
/** color for BuYu(residential part)
 */
@property (nonatomic) NSInteger clrBuYu;
/** color for middle line
 */
@property (nonatomic) NSInteger clrMiddle;
/** color for stroke index
 */
@property (nonatomic) NSInteger clrIndex;
/** font name for stroke index, default: Courier
 */
@property (nonatomic) NSString  *fontIndex;
/** background image's grid type
 */
@property (nonatomic) StrokeBack backImage;
/** time for filling speed, in seconds, default 0.02
 */
@property (nonatomic) CGFloat timeUpdate;
/** maximum update point count, default 40
 */
@property (nonatomic) NSInteger maxUpdate;
/** property to get chn code
 */
@property (nonatomic, readonly, getter=getCode) NSString *code;
/** callback interface, to query data for stroke
 */
@property (nonatomic,weak) id<StrokeDataDelegate> delegate;

-(id)init;
-(id)initWithCoder:(NSCoder *)aDecoder;
-(id)initWithFrame:(CGRect)frame;
-(void)dealloc;

-(void)close;
-(BOOL)clearShow;
-(void)setOptionForContour:(BOOL)drawContour fillContour:(BOOL)fill midline:(BOOL)drawMidline andIndex:(BOOL)drawIndex;
-(BOOL)setCode:(NSString *)sCode;
-(BOOL)fillStroke:(NSString *)sCode withDelegate:(id<PlayChnSoundDelegate>)delegate;
-(BOOL)stopFilling;

+(NSArray *)getRGBAsFromImage:(UIImage *)image atX:(int)x andY:(int)y count:(int)count;
@end
